<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/12
 * Time: 上午10:48
 * Describe:
 */

namespace console\controllers;

use console\models\Object;
use services\models\MemberScore;
use Yii;
use yii\log\Logger;


class AbcController extends \yii\console\Controller
{

    public $file;

    public function init()
    {
        set_time_limit(3000);
        ini_set('memory_limit', '4G');
        parent::init();
        #$this->file = new Logger();


    }

    public static function info()
    {

    }

    /**
     * 修复试用用户列表 redis
     */
    public function actionLiketryRedis()
    {
        $redis = \Yii::$app->redis;
        // 4308 4313 4352 4403 4404

        $product_ids = Yii::$app->db->createCommand("select distinct g.goods_id, g.product_id from d_try.order_goods g
                        inner join d_try.order_info i on i.order_info_id = g.order_info_id
                        inner join d_try.product p on p.product_id=g.product_id
                        where p.type=1
                        AND i.status NOT IN (0, 6)
                        and g.product_id <= 40000000
                        AND g.goods_id IN (20004308,20004308 ,20004313 ,20004352 ,20004403 ,20004404)

                        ORDER by g.goods_id ASC ;")
                                    ->queryAll();
        $goods_id    = '';
        foreach ($product_ids as $row) {
            echo $row['goods_id'], ' ', $row['product_id'], "\n";
            $bestry_uids = Yii::$app->db->createCommand("select  i.bestry_uid from d_try.order_goods g
inner join d_try.order_info i on i.order_info_id = g.order_info_id
inner join d_try.product p on p.product_id=g.product_id
where p.type=1
and g.product_id ={$row['product_id']} and g.goods_id = {$row['goods_id']}
AND i.status NOT IN (0, 6)")->queryColumn();
            if ($goods_id != $row['goods_id']) {
                $redis->del(Yii::$app->params['try_goods_uid'] . $row['goods_id']);
                $goods_id = $row['goods_id'];
                echo $goods_id, "\n";
            }
            foreach ($bestry_uids as $uid) {
                #    echo $uid, "\n";
                $redis->zadd(Yii::$app->params['try_goods_uid'] . $row['goods_id'], 0, $uid);
            }

        }
    }


    /**
     * 试用已打包未发货订单 退还试豆, 已执行过, 不需要再次运行
     */
    public function actionPackageA()
    {
        /*$f = fopen('/tmp/'.date('YmdHis'), 'w');
        $packages = Yii::$app->db->createCommand("
                                    SELECT
                                      no,
                                      s_uid,
                                      package_code
                                    FROM
                                      iliketry.package
                                    WHERE
                                      (STATUS = '已打包' OR STATUS = '打包中')

                                      ;")
                                 ->queryAll();
        foreach ($packages as $package) {
            if($package['no'] == 200661) continue;
            fwrite($f, "同楼打包更新，包裹{$package['package_code']}押金退回:{$package['no']}\n");
            MemberScore::addScore(500, "同楼打包更新，包裹{$package['package_code']}押金退回", $package['s_uid']);
            $orders = Yii::$app->db->createCommand("
                                        SELECT
                                          member_no,
                                          order_code
                                        FROM
                                          iliketry.package_order
                                        WHERE package_no = {$package['no']}
                                        AND is_qr = 0;")
                                   ->queryAll();
            foreach ($orders as $order) {
                fwrite($f, "包裹{$package['package_code']}订单 {$order['order_code']} 拆包退还试豆 {$order['member_no']}\n");
                MemberScore::addScore(100, "包裹{$package['package_code']}订单 {$order['order_code']} 拆包退还试豆", $order['member_no']);
            }

        }
        fclose($f);*/
    }

    public function actionPackageB()
    {
        $f = fopen('/tmp/' . date('_YmdHis'), 'w');

        /** @var \Redis $redis */
        $redis       = Yii::$app->redis;
        $package_nos = $redis->smembers('tmp_pp_pacage_try_coin_package_no');
        $condition   = "";
        if (!empty($package_nos)) {
            $condition = "AND p.no NOT IN (" . implode(',', $package_nos) . ")";
        }
        # -- and p.no = 442125
        $items = Yii::$app->db->createCommand("SELECT
                  p.no,
                  p.s_uid,
                  o.order_info_id,
                  p.package_code,
                  po.order_code
                FROM iliketry.package_order po
                  INNER JOIN iliketry.package p ON p.no = po.package_no
                  INNER JOIN d_try.order_info o ON o.order_info_id = (po.order_no + 20000000)
                WHERE po.is_qr = 0 AND p.status = '已发货' AND o.status = 5 AND o.order_info_id BETWEEN 20000000 AND 39999999
                $condition
                ORDER BY p.no ASC;")
                              ->queryAll();

        $nos = [];
        foreach ($items as $item) {
            $result = $redis->sadd('tmp_pp_pacage_try_coin', $item['order_info_id']);
            if ($result == 1) {
                MemberScore::addScore(100, "包裹{$item['package_code']}订单 {$item['order_code']} 确认收货.", $item['s_uid']);
                fwrite($f, $item['no'] . ',100,' . $item['s_uid']. ",包裹{$item['package_code']}订单 {$item['order_code']} 确认收货\n");
            }
            $nos[] = $item['no'];
        }

        $nos = array_unique($nos);
        foreach ($nos as $no) {
            $items     = Yii::$app->db->createCommand("
                SELECT package_code, order_code, package_no,(order_no+20000000) AS order_no, is_qr,member_no, is_first
                FROM iliketry.package_order WHERE package_no = $no ;")
                                      ->queryAll();
            $t         = true;
            $member_no = '';
            foreach ($items as $item) {
                $package_code = $item['package_code'];
                if ($item['is_first']) {
                    $member_no = $item['member_no'];
                }
                if ($item['is_qr'] == 0) {
                    if (!$redis->sismember('tmp_pp_pacage_try_coin', $item['order_no'])) {
                        $t = false;
                    }
                }
            }
            if ($t) {
                $result = $redis->sadd('tmp_pp_pacage_try_coin_package_no', $no);
                if ($result == 1) {
                    MemberScore::addScore(500, "同楼打包更新，包裹{$package_code}押金退回.", $member_no);
                    fwrite($f, $no . ',500,' . $member_no.",同楼打包更新，包裹{$package_code}押金退回.\n");

                }
            }

        }
        fclose($f);

    }


    /**
     * 刷新优选中的试用订单低于2000万
     */
    /*public function actionOldLiketryGoods()
    {
        $result = Yii::$app->db->createCommand("select DISTINCT order_id, goods_id, product_id
                FROM tmall1.order_goods where free_order_id >0")->queryAll();
        Yii::$app->redis_goods->del('oldLiketryOrderGoodsId');
        foreach ($result as $item) {
            Yii::$app->redis_goods->sadd('oldLiketryOrderGoodsId', implode('_', $item));
        }
    }*/


    /**
     * 老喜试订单, 更新快递号
     * d_try 已经更新过.
     * @throws \yii\db\Exception
     */
    public function actionLiketryDeliveryCode()
    {
        $package_nos = Yii::$app->db->createCommand("SELECT no, deli_code FROM iliketry.package
            where deli_code <> package_code and
            deli_code <>'' ORDER BY NO ASC ")->queryAll();
        $transaction = Yii::$app->db->beginTransaction();
        $i           = 0;
        foreach ($package_nos as $item) {
            $i++;
            if ($item['no'] < 349766) continue;
            echo $item['no'], "\n";
            $order_nos = Yii::$app->db->createCommand("
                SELECT order_no + 20000000
                FROM iliketry.package_order
                WHERE package_no = {$item['no']};")->queryColumn();
            /*-- UPDATE d_try.order_express
            -- SET delivery_code = '{$item['deli_code']}', delivery_id = 1
            -- WHERE order_info_id IN (" . implode(',', $order_nos) . ") and delivery_code='';*/
            Yii::$app->db->createCommand("
                UPDATE d_order_history.order_express
                SET delivery_code = '{$item['deli_code']}', delivery_id = 1
                WHERE order_info_id IN (" . implode(',', $order_nos) . ")
            ")->execute();
            if ($i % 5000 == 0) {
                $transaction->commit();
                $transaction = Yii::$app->db->beginTransaction();

            }
        }
        $transaction->commit();

    }

    public function actionTest()
    {
        var_dump(Yii::$app->redis->zrange('registration_1416866', 0, -1));
        echo Yii::$app->redis_index->get('help_10019');
        var_dump(Yii::$app->redis_goods->type('product_10005882'));
        echo Yii::$app->redis_comments->get('leo1');

echo "\n\n";
        var_dump(Yii::$app->redis->zrange('registration_1416866', 0, -1));
        echo Yii::$app->redis_index->get('help_10019');
        var_dump(Yii::$app->redis_goods->type('product_10005882'));
        echo Yii::$app->redis_comments->get('leo1');
        echo "\n\n";
        var_dump(Yii::$app->redis->zrange('registration_1416866', 0, -1));
        echo Yii::$app->redis_index->get('help_10019');
        var_dump(Yii::$app->redis_goods->type('product_10005882'));
        echo Yii::$app->redis_comments->get('leo1');

    }

}



